import time
import socketio
import math
import json

sio = socketio.Client(engineio_logger=True)
start_timer = None
num = 0
index = 0
total_time = 0

def cut_value():
    with open('../data/ciphertext_47.json', 'r') as f2:
        type_json = json.load(f2)  # 读取时对文件进行反序列化

    org_str = type_json
    str_len = len(org_str)
    length = 1024*512 # 512K
    n = math.ceil(str_len / length)

    global num
    num = n

    i = 0
    all_str_list = []
    while n > 0:
       split_str = org_str[i * length: (i + 1) * length]
       all_str_list.append(split_str)
       i += 1
       n -= 1
    return all_str_list

result_list = cut_value()

def send_ping(i):
    global start_timer
    start_timer = time.time()
    sio.emit('ping_from_client',  result_list[i])

def send_num(num):
    global start_timer
    start_timer = time.time()
    sio.emit('ping_from_client', num)

@sio.event
def connect():
    print('connected to server')
    global index
    send_ping(index)


@sio.event
def pong_from_server():
    global start_timer
    latency = time.time() - start_timer
    #print('latency is {0:.2f} ms'.format(latency * 1000))

    global total_time
    total_time += latency
    print('当前所花总时间 {0:.2f} ms'.format(total_time * 1000))

    sio.sleep(1)

    global index
    index = index + 1
    send_ping(index)
    print("发送第{}个数据".format(index))

if __name__ == '__main__':
    sio.connect('http://192.168.1.107:5010')
    sio.wait()
